<template>
  <div class="char4">
    <!-- 自定义一个div来装我们的图 -->
    <div id="main" style="width: 90%; height: 800px"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  data() {
    return {
      option: {
        title: {
          text: "世界人口总量",
          subtext: "数据来自网络",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {
          data: ["2011年", "2012年"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
          boundaryGap: [0, 0.01],
        },
        yAxis: {
          type: "category",
          data: ["巴西", "印尼", "美国", "印度", "中国", "世界人口(万)"],
        },
        series: [
          {
            name: "2011年",
            type: "bar",
            data: [18203, 23489, 29034, 104970, 131744, 630230],
          },
        ],
      },
    };
  },
  methods: {
    draw3() {
      //通过echarts初始化我们的div
      let myChart = echarts.init(document.getElementById("main"));
      //为这个echarts的图添加属性
      myChart.setOption(this.option, true);
    },
  },
  watch: {
    //数据变化时自动重画，在控制台修改message,会自动重画
    message: function () {
      this.draw3();
    },
  },
  mounted() {
    //页面加载的时候，调用画图方法，画图
    this.draw3();
  },
};
</script>

<style scoped>
.char3 {
  height: 100%;
  width: 100%;
  text-align: center;
  line-height: 160px;
}
</style>
